﻿using System.Web;
using System.Web.Optimization;
using System.Xml.Linq;
using System.Linq;

namespace Company.UI
{
    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));
            bundles.Add(new ScriptBundle("~/bundles/jqueryuploadify").Include(
                "~/Scripts/jquery.uploadify-{version}.js"
                ));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));

            bundles.Add(new StyleBundle("~/bundles/admin/css").Include(
                "~/Content/Admin/css/bootstrap-cerulean.css",
                "~/Content/Admin/css/bootstrap-responsive.css",
                "~/Content/Admin/css/charisma-app.css",
                "~/Content/Admin/css/jquery-ui-{version}.custom.css",
                "~/Content/Admin/css/fullcalendar.css",
                "~/Content/Admin/css/fullcalendar.print.css",
                "~/Content/Admin/css/chosen.css",
                "~/Content/Admin/css/uniform.default.css",
                "~/Content/Admin/css/colorbox.css",
                "~/Content/Admin/css/jquery.cleditor.css",
                "~/Content/Admin/css/jquery.noty.css",
                "~/Content/Admin/css/noty_theme_default.css",
                "~/Content/Admin/css/elfinder.min.css",
                "~/Content/Admin/css/elfinder.theme.css",
                "~/Content/Admin/css/jquery.iphone.toggle.css",
                "~/Content/Admin/css/opa-icons.css",
                "~/Content/Admin/css/uploadify.css"
                ));
            bundles.Add(new ScriptBundle("~/bundles/admin/js").Include(
                "~/Content/Admin/js/bootstrap-transition.js",
                "~/Content/Admin/js/bootstrap-alert.js",
                "~/Content/Admin/js/bootstrap-modal.js",
                "~/Content/Admin/js/bootstrap-dropdown.js",
                "~/Content/Admin/js/bootstrap-scrollspy.js",
                "~/Content/Admin/js/bootstrap-tab.js",
                "~/Content/Admin/js/bootstrap-tooltip.js",
                "~/Content/Admin/js/bootstrap-popover.js",
                "~/Content/Admin/js/bootstrap-button.js",
                "~/Content/Admin/js/bootstrap-collapse.js",
                "~/Content/Admin/js/bootstrap-carousel.js",
                "~/Content/Admin/js/bootstrap-typeahead.js",
                "~/Content/Admin/js/bootstrap-tour.js",
                "~/Content/Admin/js/jquery.cookie.js",
                "~/Content/Admin/js/fullcalendar.js",
                "~/Content/Admin/js/jquery.dataTables.js",
                "~/Content/Admin/js/excanvas.js",
                "~/Content/Admin/js/jquery.flot.js",
                "~/Content/Admin/js/jquery.flot.pie.js",
                "~/Content/Admin/js/jquery.flot.stack.js",
                "~/Content/Admin/js/jquery.flot.resize.js",
                "~/Content/Admin/js/jquery.chosen.js",
                "~/Content/Admin/js/jquery.uniform.js",
                "~/Content/Admin/js/jquery.colorbox.js",
                "~/Content/Admin/js/jquery.cleditor.js",
                "~/Content/Admin/js/jquery.noty.js",
                "~/Content/Admin/js/jquery.elfinder.js",
                "~/Content/Admin/js/jquery.raty.js",
                "~/Content/Admin/js/jquery.iphone.toggle.js",
                "~/Content/Admin/js/jquery.autogrow-textarea.js",
                "~/Content/Admin/js/jquery.history.js",
                "~/Content/Admin/js/charisma.js"
                ));

            //通过配置文件加载
            var doc = XElement.Load(HttpContext.Current.Server.MapPath("~/Config/BundleConfig.xml"));
            var bundles_ = (from m in doc.Element("scripts").Elements("script") let bundle = new ScriptBundle(m.Attribute("path").Value).Include(m.Elements("file").Select(file => file.Value).ToArray()) select bundle).Concat(from m in doc.Element("styles").Elements("style") let bundle = new StyleBundle(m.Attribute("path").Value).Include(m.Elements("file").Select(file => file.Value).ToArray()) select bundle);
            bundles_.Select(m=>bundles.Add(m) is object);
        }
    }
}